app.controller('SalesShiftAddCtrl', function ($scope, $modalInstance, toaster, param,ajax) {

    $scope.param = param;
    if ($scope.param.data == null) {
        $scope.param.data = {};
    }

    //起始客运站列表读取
    (function() {
        ajax.call(new Req.GET({
            resource: 'starting/bk/list'
        })).then(function(result) {
            $scope.starList = result.result;
        }, function() { });
    })();
    
	var inserter = new Req.POST({ resource: $scope.resource });
    
	$scope.list=[];
    $scope.queryMap = {};
    $scope.timeDeparture={};
	$scope.query = function () {
        if($scope.starList.selected &&$scope.queryMap.terminal&&$scope.timeDeparture.startTime){
        	var starTime = $scope.timeDeparture.startTime.pattern("yyyy-MM-dd");
	    	inserter.fields={"stationId":$scope.starList.selected.id,"terminal":$scope.queryMap.terminal,"startTime":starTime};
	  		inserter.resource = 'shift/bk';
			ajax.call(inserter).then(function (result) {
               if(result.code!=500){
   				  if (result.result.shifts.length>0) {
					 $scope.list=result.result.shifts;
				  } else {
					 $scope.list=[];
					toaster.pop('info', '提示','暂无该班次信息！');
				  }
               }else{
            	   toaster.pop('error', '错误', '获取车次信息失败，请确认查询条件是否正确！'); 
               }
			}, function () {
				toaster.pop('error', '错误', '获取车次信息失败，请确认查询条件是否正确！');
			});
        }else{
        	toaster.pop('warning', '提示', '请输入查询条件！ ');
        	return;
        } 
	};
    
	$scope.checkShift = [];
	$scope.checks = false;//默认未选中
	$scope.ckAll = false;//默认未选中
	$scope.choseArr = [];

	/*单选*/
	$scope.check = function (val) {
		if ($scope.checkShift.indexOf(val) < 0) {
			$scope.checkShift.push(val);
		} else {
			$scope.checkShift.splice($scope.checkShift.indexOf(val), 1);
		}
	};

	/*全选*/
	$scope.checkAll = function (c, v) {
		if (c == true) {
			$scope.checks = true;
			$scope.choseArr = v;
			//添加所选Id
			for (var i = 0; i < $scope.choseArr.length; i++) {
				$scope.checkShift.push($scope.choseArr[i]);
			}
		} else {
			$scope.checks = false;
			$scope.checkShift = [];
		}
	};
    
    $scope.ok = function() {
    	console.log($scope.checkShift);
       if($scope.checkShift.length>0){
    	   $scope.param.ids=$scope.checkShift;
    	   $scope.param.startId=$scope.starList.selected.id;
    	   $modalInstance.close(param);
       }else{
      	 toaster.pop('warning', '提示', '请勾选班次！ ');
    	 return;
       }
    };
    
    
    $scope.cancel = function () {
        $modalInstance.dismiss('cancel');
    };
});